Wednesday, July 30, 2008

QueryString in .htaccess - Match Question Mark in rewrite rule

I am changing my development structure to have rewrite urls for fuse/action
and I thought it's cool if I represent all URLs like google (http://www.google.co.in/search?hl=en&client=firefox-a)

so http://mysite.com?index.php?action=search&q=vikrant should look like http://mysite.com/search?q=vikrant

for this purpose I have first tried a simple rule to match anything after "/".

RewriteEngine On
RewriteRule ^(.*)$ index.php?action=$1 [L]

but when I print GET variables then it s only giving me the part before "?" (that was strange)

Then after googling a bit I just got the hint that Mod Rewrite do not matches string after "?"
because it's treating it as %{QUERYSTRING}, hence I have created following rule to achieve the result which is running perfect for me

RewriteEngine On
RewriteRule ^/?([^/][^\./]*)[:;,\.]*$ index.php?action=$1&%{QUERY_STRING} [L]


10 comments:

Anonymous said...

Works brilliantly. Thank you

Unknown said...

I have got the following URL witch isnt workin either:



RewriteRule ^(.*)/photos/\?level=(.*)&id=(.*)$ photos.php?lang=$1&level=$2&id=$3 [L]
RewriteRule ^(.*)/photos/\?level=(.*)$ photos.php?lang=$1&level=$2 [L]
RewriteRule ^(.*)/photos/$ photos.php?lang=$1 [L]

http://www.alsacorp.eu/fr/photos/?level=collection&id=2

I thought i had to escape ? with \ ? but the page still doesnt work, ccan you help me out with how i should do it?

Richard Lee said...

Cheers mate,

I had spent half on this before I found this page.

b said...

Thank you!

Christian said...

Ah, thanks. This was making me crazy :-)

Nidaan said...

Query strings are not part of a URL.

You must use
RewriteCond %{QUERY_STRING} ^level=(.*)&id=(.*)$
RewriteRule ^(.*)/photos/$ photos.php?lang=$1&level=%1&id=%2 [L]

Tian said...

I had a full week searching for something like this

THANK YOU!!!

Tian said...

I had a full week googling about this...

THANK YOU!!

PppStuDio said...

Cheer. That's help a lot!
Thank you

ildannolo said...

you genius - just saved me a lot of time!